import json
import os

from django.db import models
from django.contrib.auth.models import AbstractUser

from routing_django.settings import MEDIA_ROOT


class FileModel(models.Model):
    datetime = models.DateTimeField()
    user = models.TextField()
    file_field = models.FileField(upload_to='excel')
    # rows_num = models.IntegerField()

    # def save(self, *args, **kwargs):
    #     self.columns_num = kwargs["rows_num"]
    #     super(FileModel, self).save(*args, **kwargs)

    def __str__(self):
        return f'{self.user}: {self.file_field.name}'


class OrderModel(models.Model):
    datetime = models.DateTimeField()
    user = models.TextField()
    file_field = models.FileField(upload_to='requests')

    def __str__(self):
        return f'{self.user}: {self.file_field.name}'

    def get_json(self):
        return json.loads(self.file_field.read())


class RoutesModel(models.Model):
    datetime = models.DateTimeField()
    user = models.TextField()
    file_field = models.FileField(upload_to='routes')

    def __str__(self):
        return f'{self.user}: {self.file_field.name}'


class RouteFigsModel(models.Model):
    datetime = models.DateTimeField()
    user = models.TextField()
    html = models.TextField()

    def __str__(self):
        return f'{self.user}: {self.datetime.__str__()}'


class SheetsModel(models.Model):
    datetime = models.DateTimeField()
    user = models.TextField()
    file_field = models.FileField(upload_to='sheets')

    def __str__(self):
        return f'{self.user}: {self.file_field.name}'

    def get_excel(self):
        excel_path = os.path.join(MEDIA_ROOT, self.file_field.name)
        return open(excel_path, 'rb')


class GvkmUser(AbstractUser):
    city = models.TextField()